package by.ins.life.simpleimpl;



import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import by.ins.life.ILife;
import by.ins.life.ILifeInitializer;
import by.ins.life.ISolver;
import by.ins.life.evolution.ILifeEvoluter;
import by.ins.life.evolution.ISetEvoluter;
import by.ins.life.evolution.TestGoalsProviders;
import by.yativem.commons.task.AbstractManagedTask;


public class LifeEvolutor<T> extends AbstractManagedTask implements  ILifeEvoluter<T> {
private static final Logger log = LogManager.getLogger(LifeEvolutor.class);
	private ILife<T> life;
	private ISetEvoluter<T> setEvoluter;
	private ILifeInitializer<T> lifeInitializer;
	private ISolver<T> solver;
	private TestGoalsProviders<T> testGoalsProviders; 
//	private boolean isLifeInit = false;

	
	private static final long serialVersionUID = 1L;
	
	/*public LifeEvolutor(Life life){
		this.life = life;
	}
	*/
	public LifeEvolutor(){
		
	}
	
	/* (non-Javadoc)
	 * @see by.ins.life.simpleimpl.ILifeEvoluter#evolute()
	 */
	@Override
	public void evolute(){
		setEvoluter.evoluteSet(life);
	}
	
	@Override
	public void evolute(int evolutionCount) {
		
		for(int i =0 ; i < evolutionCount; i ++){
			evolute();
		}
		
	}
	

	/* (non-Javadoc)
	 * @see by.ins.life.simpleimpl.ILifeEvoluter#getLife()
	 */
	@Override
	public ILife<T> getLife() {
		return life;
	}

	/* (non-Javadoc)
	 * @see by.ins.life.simpleimpl.ILifeEvoluter#setLife(by.ins.life.simpleimpl.Life)
	 */
	@Override
	public void setLife(ILife<T> life) {
		this.life = life;
	}

	/* (non-Javadoc)
	 * @see by.ins.life.simpleimpl.ILifeEvoluter#getSetEvoluter()
	 */
	@Override
	public ISetEvoluter<T> getSetEvoluter() {
		return setEvoluter;
	}

	/* (non-Javadoc)
	 * @see by.ins.life.simpleimpl.ILifeEvoluter#setSetEvoluter(by.ins.life.evolution.ISetEvoluter)
	 */
	@Override
	public void setSetEvoluter(ISetEvoluter<T> evoluter) {
		this.setEvoluter = evoluter;
	}

	/* (non-Javadoc)
	 * @see by.ins.life.simpleimpl.ILifeEvoluter#getSolver()
	 */
	@Override
	public ISolver<T> getSolver() {
		return solver;
	}

	/* (non-Javadoc)
	 * @see by.ins.life.simpleimpl.ILifeEvoluter#setSolver(by.ins.life.ISolver)
	 */
	@Override
	public void setSolver(ISolver<T> solver) {
		this.solver = solver;
	}

	@Override
	public void doTask() {
		lifeInitializer.initLife(this.life);
		while(true){
			checkAllCommands();
			setEvoluter.evoluteSet(this.life);
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				log.error("", e);
			}
		}
		
	}
	@Override
	public ILifeInitializer<T> getLifeInitializer() {
		return lifeInitializer;
	}
	@Override
	public void setLifeInitializer(ILifeInitializer<T> lifeInitializer) {
		this.lifeInitializer = lifeInitializer;
	}
	@Override
	public String getName() {
		return getTaskName();
	}
	@Override
	public void setName(String name) {
		setTaskName( name);
	}
	@Override
	public TestGoalsProviders<T> getTestGoalProviders() {
		return testGoalsProviders;
	}
	@Override
	public void setTestGoalProviders(TestGoalsProviders<T> testGoalProviders) {
		this.testGoalsProviders = testGoalProviders;
	}
	

}
